package krow.dev.addetector.view;

import krow.dev.addetector.util.DisplayUtil;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.view.ViewHelper;

public class FloatingButton extends Button {

	private Paint mButtonPaint;
	private Paint mDrawablePaint;
	private Bitmap mBitmap;
	private int mScreenHeight;
	private float mCurrentY;;
	private boolean mHidden = true;

	public FloatingButton(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
		initialize(context);
	}

	public FloatingButton(Context context) {
		super(context);
		initialize(context);
	}

	public void setColor(int color) {
		mButtonPaint.setColor(color);
		invalidate();
	}

	public void setDrawable(Drawable drawable) {
		mBitmap = ((BitmapDrawable) drawable).getBitmap();
		invalidate();
	}

	public void setCurrentY(int y) {
		mCurrentY = y;
	}

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	public void initialize(Context context) {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		}
		setWillNotDraw(false);
		setBackgroundColor(Color.TRANSPARENT);

		mButtonPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mButtonPaint.setColor(Color.WHITE);
		mButtonPaint.setStyle(Paint.Style.FILL);
		mButtonPaint.setShadowLayer(10.0f, 0.0f, 3.5f, Color.argb(100, 0, 0, 0));
		mDrawablePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		invalidate();

		DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
		int height = displayMetrics.heightPixels;

		mScreenHeight = height;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		setClickable(true);
		
		canvas.drawCircle(
				getWidth() / 2, 
				getHeight() / 2, 
				(float) (getWidth() / 2.6), 
				mButtonPaint);
		
		canvas.drawBitmap(
				mBitmap, 
				(getWidth() - mBitmap.getWidth()) / 2, 
				(getHeight() - mBitmap.getHeight()) / 2, 
				mDrawablePaint);
	}

	@SuppressLint("ClickableViewAccessibility")
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_UP) {
			ViewHelper.setAlpha(this, 1.0f);
		} else if (event.getAction() == MotionEvent.ACTION_DOWN) {
			ViewHelper.setAlpha(this, 0.6f);
		}
		return super.onTouchEvent(event);
	}

	public int dpToPx(int dp) {
		DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
		int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
		return px;
	}

	public void hide() {
		if (mHidden) {
			return;
		}

		int[] location = new int[2];
		getLocationOnScreen(location);

		ObjectAnimator hideAnimator = ObjectAnimator.ofFloat(this, "y", mScreenHeight);
		hideAnimator.setInterpolator(new AccelerateInterpolator());
		hideAnimator.start();
		mHidden = true;
	}

	public void show() {
		if (!mHidden) {
			return;
		}

		ObjectAnimator showAnimator = ObjectAnimator.ofFloat(this, "y", mCurrentY);
		showAnimator.setInterpolator(new DecelerateInterpolator());
		showAnimator.addListener(mShowAnimatorListener);
		showAnimator.start();
		
		mHidden = false;
	}

	private AnimatorListener mShowAnimatorListener = new AnimatorListener() {
		
		@Override
		public void onAnimationStart(Animator animation) {
			
		}
		
		@Override
		public void onAnimationRepeat(Animator animation) {
			
		}
		
		@Override
		public void onAnimationEnd(Animator animation) {
			if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
				clearAnimation();
				int size = DisplayUtil.getDimensionToPixel(getContext(), 72F);
				int margin = DisplayUtil.getDimensionToPixel(getContext(), 16F);

				RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(size, size);
				layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
				layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
				layoutParams.rightMargin = margin;
				layoutParams.bottomMargin = margin;
				setLayoutParams(layoutParams);
			}
		}
		
		@Override
		public void onAnimationCancel(Animator animation) {
			
		}
	}; 
}